
from apps.chat.conversation.models import Conversation
from apps.chat.conversation.dao import ConversationDao
from apps.chat.message.models import Message
from apps.chat.message.dao import MessageDao
from apps.system.user.models import User

# currentUser： 发送者
# messageType:  system，custom
# contentType: text, iamge
# content: 内容
# params：
def sendMessage(befromUser, toUser, messageType, params):
    print('发送消息', befromUser, toUser, messageType, params)
    try:
        messageType = messageType or 'system'
        contentType = params.get('contentType') or 'text'
        message = None
        conversation = None
        if messageType == 'system':
            # 首先创建一条消息
            data = {"befrom": toUser.id, "to": toUser.id, "content": params.get('content'), "contentType": contentType,
                    "messaggeType": messageType}
            message = MessageDao().create(data)
            # 首先查找双方是否有会话
            conversationList = ConversationDao().findAll().filter(userA=toUser, userB=toUser)
            if conversationList.exists():
                conversation = conversationList.first()
                conversation.lastMessage = message
                conversation.save()
            else:
                conversation = ConversationDao().create({"userA": toUser.id, "userB": toUser.id, "lastMessage": message, "conversationType": messageType})

        # 关联该消息的会话ID
        message.conversationId = conversation
        message.save()
        return {"conversationId": conversation.id, "messageId": message.id}
    except Exception as e:
        print('发送消息异常', e)
        return {"message": '发送异常'}



def sendMessage1(currentUser, params):
    messageType = params.get('messageType') if params.get('messageType') else 'custom'
    contentType = params.get('contentType') if params.get('contentType') else 'text'

    if messageType == 'system':
        #首先创建一条消息
        data = {"befrom": currentUser, "to": currentUser, "content": params.get('content'), "contentType": contentType, "messaggeType": messageType}
        message = Message.objects.create(**data)
        #首先查找双方是否有会话
        conversationList = Conversation.objects.filter(userA=currentUser, userB=currentUser, is_delete=0)
        conversation = None
        if conversationList.exists():
            conversation = conversationList.first()
            conversation.lastMessage = message
            conversation.save()
        else:
            conversation = Conversation.objects.create(**{"userA": currentUser, "userB": currentUser, "lastMessage": message, "conversationType": messageType})
    else: #默认消息类型是用户消息
        targetUser = UserModel.objects.get(id=params.get("userId"))        
        data = { "befrom": currentUser, "to": targetUser, "content": params.get('content'), "contentType": contentType}
        # if params.get('scene') == 'letter':
        #     data['is_delete'] = 1
        message = Message.objects.create(**data)
        #首先查找双方是否有会话
        conversationList1 = Conversation.objects.filter(userA=currentUser, userB=params.get('userId'), is_delete=0)
        conversationList2 = Conversation.objects.filter(userA=params.get('userId'), userB=currentUser, is_delete=0)
        conversation = None
        if conversationList1.exists() > 0:
            conversation = conversationList1.first()
            conversation.lastMessage = message
            conversation.save()
        elif conversationList2.exists() > 0:
            conversation = conversationList2.first()
            conversation.lastMessage = message
            conversation.save()
        else:
            conversation = Conversation.objects.create(**{"userA": currentUser, "userB": targetUser, "lastMessage": message})
    # 关联该消息的会话ID
    message.conversation = conversation
    message.save()
    return {"conversationId": conversation.id, "messageId": message.id}